#include "fatitem.h"
typedef enum MovePos
{
    QUARTER_FMP,
    HALF_FMP,
    THREEQ_FMP,
    ONE_FMP,
}MOVEPOS;

FatItem::FatItem(const MANTYPE& manTp):
    ManItem(manTp),
    m_bRuning(false),
    m_movePos(QUARTER_FMP)
{
    m_FloatSpeed = 0.2;

    m_animamap[GO_TP] = 3;
    m_animamap[CATCH_TP] = 6;
    m_animamap[TURN_TP] = 9;
    m_animamap[DROP_TP] = 11;
    m_animamap[DROPING_TP] = 13;
    m_animamap[UP_TP] = 16;
    m_animamap[RUN_SQUAT_TP] = 19;

    SetDetectX();
}

void FatItem::logic()
{
    if(m_bRun)
    {
        switch(m_mananimatp)
        {
        case TURN_TP:
        {
            if(m_x == m_animamap[m_mananimatp] * m_ManSize.width())
            {
                SetAnimaType(m_bRuning ? RUN_SQUAT_TP : GO_TP);
//                SetAnimaType(GO_TP);
            }
            else
            {
                m_x += m_ManSize.width();
            }
        }
            break;
        case GO_TP:
        {
            if(m_x == m_animamap[m_mananimatp] * m_ManSize.width())
            {
                m_x = 0;
            }
            else
            {
                m_x += m_ManSize.width();
            }

            Go();
        }
            break;
        case CATCH_TP:
        {
            if(m_x == m_animamap[m_mananimatp] * m_ManSize.width())
            {
                m_x = (m_animamap[m_mananimatp - 1] + 1) * m_ManSize.width();
            }
            else
            {
                m_x += m_ManSize.width();
            }

            Cover();
        }
            break;
        case RUN_SQUAT_TP:
        {
            if(m_x == m_animamap[m_mananimatp] * m_ManSize.width())
            {
                m_x = (m_animamap[m_mananimatp - 1] + 1) * m_ManSize.width();
            }
            else
            {
                m_x += m_ManSize.width();
            }

            Run();
        }
            break;
        case DROPING_TP:
        {
            if(m_x == m_animamap[m_mananimatp] * m_ManSize.width())
            {
                m_x = (m_animamap[m_mananimatp - 1] + 1) * m_ManSize.width();
            }
            else
            {
                m_x += m_ManSize.width();
            }

            Droping();
        }
            break;
        case UP_TP:
        {
            if(m_x == m_animamap[m_mananimatp] * m_ManSize.width())
            {
                m_bRuning = true;
                if(IsTurn())
                {
                    Turn();
                }
                else
                {
                    SetAnimaType(RUN_SQUAT_TP);
//                    SetAnimaType(GO_TP);
                }
            }
            else
            {
                m_x += m_ManSize.width();
            }
        }
            break;
        case DROP_TP:
        {
            if(m_x == m_animamap[m_mananimatp] * m_ManSize.width())
            {
                SetAnimaType(DROPING_TP);
            }
            else
            {
                m_x += m_ManSize.width();
            }
        }
            break;
        default:
            break;
        }

        if(m_bdrawlink)
            DrawLink();

        update();
    }
}

void FatItem::SetDetectX()
{
    m_detectX = m_bleft ? static_cast<qreal>(qrand() % 320) :
                          static_cast<qreal>(qrand() % 320) + 320;
}

void FatItem::Go()
{
    if(m_bleft)
    {
        if(x() <= m_detectX)
        {
            m_detectX = static_cast<qreal>(qrand() % 320) + 320;
            Turn();
        }
        else
        {
            setX(x() - 4);
        }
    }
    else
    {
        if(x() >= m_detectX)
        {
            m_detectX = static_cast<qreal>(qrand() % 320);
            Turn();
        }
        else
        {
            setX(x() + 4);
        }
    }
}

bool FatItem::IsTurn()
{
    bool bTurn;
    if(x() <= 320.0)
    {
       // bTurn = m_bleft;
        bTurn = !m_bleft;
    }
    else
    {
        //bTurn = !m_bleft;
        bTurn = m_bleft;
    }

   // m_detectX = (x() <= 320.0) ? static_cast<qreal>(qrand() % 320) + 320:
   //                              static_cast<qreal>(qrand() % 320) ;

    return bTurn;
}

void FatItem::Run()
{
    if(x() <= 320.0)
    {
        if(x() <= 0.0)
        {
            SetAnimaType(NULL_TP);
            setVisible(false);
        }
        else
        {
            setX(x() - 4.0);
        }
    }
    else
    {
        if(x() >= 640.0)
        {
            SetAnimaType(NULL_TP);
            setVisible(false);
        }
        else
        {
            setX(x() + 4.0);
        }
    }
}
